﻿"user strict";

var CodeCube = function () {
    var accessToken = 'UIBnH_ACCESS_TOKEN';
    var loading = new KTDialog({ 'type': 'loader', 'placement': 'top center', 'message': '努力加载中 ...' });
    var maincontent = $('#kt_content');
    var amenu = $('#asidemenu');
    var _init = function () {
        $(window).bind('hashchange', function () { BeaverDam.ChangeHash(); });
        $.ajax({
            url: './js/asidemenu.html',
            success: function (d) {
                amenu.html(d);
                $('.kt-menu__item:not(.kt-menu__item--submenu) > a').bind('click', function () {
                    $('#asidemenu .kt-menu__item--here').removeClass('kt-menu__item--here');
                    $('#asidemenu .kt-menu__item--active').removeClass('kt-menu__item--active');
                    $(this).parent().addClass('kt-menu__item--active');
                });
            }
        });
    };
    var _changeHash = function () {
        let hash = window.location.hash.substring(1);
        if (hash.length > 1) {
            loading.show();
            fetch(hash)
                .then(r => r.text())
                .then(d => { loading.hide(); _setMainContent(d); })
                .catch(e => { loading.hide(); console.log('Oops,error', e); });

        }
    }
    var _setMainContent = function (html) {
        maincontent.html(html);
    }
    var _tool = {
        getToken: function () {
            var token = tool.GetSession(accessToken);
            return token != null ? { 'Authorization': 'Bearer ' + token } : null;
        },
        setToken: function (token) {
            tool.SetSession(accessToken, token);
        },
        SetSession: function (key, options) {
            localStorage.setItem(key, JSON.stringify(options));
        },
        GetSession: function (key) {
            var obj = localStorage.getItem(key);
            if (obj !== null) {
                return JSON.parse(obj);
            }
            return null;
        }
    },
        _bdtable = function (o) {
            var datatable = $(o.elem).KTDatatable({
                data: {
                    type: 'remote',
                    source: {
                        read: {
                            url: o.url,
                            headers: tool.getToken(),
                            map: function (raw) {
                                // sample data mapping
                                var dataSet = raw;
                                if (typeof raw.data !== 'undefined') {
                                    dataSet = raw.data;
                                }
                                return dataSet;
                            },
                        },
                    },
                    pageSize: 10,
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true,
                },
                // layout definition
                layout: {
                    scroll: false,
                    footer: false,
                },

                // column sorting
                sortable: true,

                pagination: true,

                search: {
                    input: o.searchelem,
                },

                // columns definition
                columns: o.cols,
            });
            return datatable;
        },
        _ajax = function (o) {
            $.ajax({
                url: o.url,
                headers: tool.getToken(),
                type: o.type === undefined ? 'POST' : o.type,
                contentType: o.contentType === undefined ? 'application/json' : o.contentType,
                data: o.data === undefined ? null : JSON.stringify(o.data),
                success: function (res, status, xhr, $form) {
                    console.log(res);
                    o.success(res, status, xhr, $form);
                }
            });
        },
        _ajaxsubmit = function (o) {
            var d = {};
            for (var i in o.data) {
                d[o.data[i].name] = o.data[i]['value'];
            }
            console.log(d);
            $.ajax({
                url: o.url,
                headers: tool.getToken(),
                type: 'POST',
                contentType: o.type === undefined ? 'application/json' : o.type,
                data: JSON.stringify(d),
                success: function (res, status, xhr, $form) {
                    console.log(res);
                    o.success(res, status, xhr, $form);
                }
            });
        }
    return {
        init: function () {
            loading.show();
            _init();
            loading.hide();
            _changeHash();
        },
        SetMainContent: function (html) { _setMainContent(html); },
        ChangeHash: _changeHash,
        tool: _tool,
        bdtable: function (o) { return _bdtable(o); },
        ajax: function (o) { return _ajax(o); },
        ajaxsubmit: function (o) { ajaxsubmit(o); }
    };
}();

jQuery(document).ready(function () {
    CodeCube.Init();
});